// 06shoppingCart/shoppingCart.js
var co = require("../resource/js/common.js")
var M = require("../resource/js/md5.js")
var app = getApp()
var _this, _data
var count = 0
var allRadioCount = 0
var secList = [] //选中的物品

Page({
   /* 页面的初始数据*/
   data: {
      empty: false, //购物车是否为空
      realHeight: '', //显示区域高度
      isShow: false, //为空时显示
      info: '', //为空时返回后台信息
      order: [], //商店数量
      showOrder: [], //商品数量
      EditBtnInfo: '编辑商品', //顶部编辑商品按钮
      isEditBtn: false, //点击显示编辑按钮
      isSettlement: true, //点击编辑按钮后隐藏底部结算
      allRadio: 0, //底部全选按钮
      shopRadio: [], //商店选择数组
      productRadio: [], //商品选择数组
      totalProducts: 0.00, //商品总价
      totalCounts: 0, //商品总量
      // oldCount: '',                       //记录当前点击的商品原个数
      idx: '', //商店循环下边
      isSpecification: 0, //商品规格弹窗是否显示
      Specification: [], //商品规格属性
      isPlus: [], //增加商品后改变减按钮颜色
      allCounts: '0',
      all: "all",
   },
   /* 生命周期函数--监听页面加载*/
   onLoad: function(options) {
      _this = this
      _data = this.data
      // 获取设备屏幕高度
      var sys = wx.getSystemInfoSync()
      this.setData({
         realHeight: sys.windowHeight
      })
      //设置监听
      app.setReloadShopCart(function(res) {
         // console.log(res)
         _this.getShopingCart()
      })
   },
   onShow: function() {
      _this.getShopingCart()
      this.setData({
         allRadio: 0,
         totalCounts: 0,
         totalProducts: '0.00',
         allRadioCount: 0
      })
      secList=[]
      count = 0
      allRadioCount = 0
   },
   // 结算
   settleAccounts: function(e) {
      var id = e.currentTarget.id
      var pList = _data.productRadio
      var sList = _data.order
      //console.log('e',e)
      console.log('pList', pList)
      //console.log('sList', sList)
      var param = []
      // console.log("【shopRadio】", this.data.shopRadio)
      if (id == "0") {

      } else {
         for (var key in pList) {
            var map = pList, 
               a = []
            //console.log('map',map)
            //console.log("【第一个】",newOrder)
             for (var keys in map) {
               if (map[keys] == 1) {
                  //console.log('key',key)
                  var newOrder = this.data.userOrder
                  //console.log('order', this.data.userOrder)
                  //console.log('key',key)
                  //console.log("【】",newOrder)
                  var orderCartList = newOrder.CartList[keys]
                  //console.log('xxx', orderCartList)
                  var obj = orderCartList
                  a.push(orderCartList)
                  //console.log("【a】",key,keys,a)
               }
            }
            //console.log('newOrder',newOrder)            
         }
         if (a.length > 0) {
            newOrder.Order_CartList = a
            //newOrder = a
            param.push(newOrder)

         }
         //console.log('param',param)
         // console.log("【取出被选中的商品详情param】===", param)
         // console.log("【取出被选中的商品详情】===", this.data.order)
         var order = JSON.stringify(param)
         //console.log('order====',order)

         wx.navigateTo({
            url: '/06shoppingCart/01orderBefor/orderBefor?param=' + order,
         })
         this.getShopingCart()
      }
      // console.log("【点击了结算】===", id)
   },
   // // 删除商品和移至收藏夹
   deleteANDinsertProduct: function(e) {
      var e_id = e.currentTarget.id //获取点击判断删除还是收藏
      // console.log("【获取点击id】===", e_id)
      var SL = _data.order
      console.log('SL',SL)
      var PList = [] // 全选商品数组
      // 全选状态删除
      if (_data.allRadio == 1) {
         for (var i in SL) {
            //for (var j in SL[i]) {
               // console.log("【获取商品id】===", SL[i][j].Product_ID)
            PList.push(SL[i].Cart_ID)
            //}
         }
      } else { //点选状态删除
         var PR = _data.productRadio
         for (var i in PR) {
           // for (var j in PR[i]) {
               if (PR[i] == 1) {
                  PList.push(SL[i].Cart_ID)
               }
           // }
         }
      }
      console.log('PList',PList)
      PList = PList.join(',')
      var param = {
         User_ID: app.Data.userInfo.User_ID
      }
      var url = ''
      if (e_id == 'delete') {
         url = 'jyhs/Cartmg/deleteShopCartPro'
         param.Cart_IDS = PList
      }
     
      // else if (e_id == 'insert') {
      //    url = 'favourite/Famg/insertFavourite'
      //    param.Products_ID = PList
      // }
      console.log('param', param)
      co.getMealApi(url, param, function(res) {
         console.log('res',res)
         if(res.code == 0){
            _this.setData({
               totalCounts: 0,
               totalProducts: '0.00',
               isEditBtn: false
            })
            _this.getShopingCart()
         }
         
         
         // console.log("获取返回信息===", res.msg)
      })
   },
   // // 点击关闭规格弹窗
   // closeSpecification: function() {
   //    this.setData({
   //       isSpecification: 0
   //    })
   // },
   // // 点击商品规格打开弹窗
   // openSpecification: function(e) {
   //    // console.log("【获取商品id】===", e.currentTarget.dataset.info)
   //    var info = e.currentTarget.dataset.info
   //    this.setData({
   //       isSpecification: 1,
   //       Specification: info
   //    })
   // },
   //商品数量加减按钮
   plusSubtractCount: function(e) {
      var idx = e.currentTarget.dataset.idx //点击那个商品的下标
      var index = e.currentTarget.id //判断加减按钮
      console.log('e', e)
      // 更新购物车数据
      var orders = _this.data.order[idx]
      var num = parseInt(_this.data.order[idx].num)
      console.log('num', _this.data)
      if (index == 'plus') {
         num = num + 1
      }
      if (index == 'subtract') {
         if (num > 1) {
            num = num - 1
         }
      }
      var param = {
         Cart_ID: _this.data.order[idx].Cart_ID,
         num: num
      }
      co.getMealApi('jyhs/Cartmg/updateShopCartProNum', param, function(res) {
         if (res.code == 0) {
            //co.toastAllText(res.msg)
            _this.getShopingCart()
         } else {
            co.toastAllText('数量不能为0')
         }
      })
   },
   // 购物车点击选择
   onRadio: function(e) {
      var index = e.currentTarget.id
      var oneRedio = _this.data.order
      var money = 0
      //console.log('oneRedio', oneRedio)
      //console.log('index', index)

      if (index == "all") { //全选

         var Radio = []
         
         //console.log(5)
         if (_this.data.allRadio == 0) {
            for (var key in oneRedio) {
               Radio.push(1)
               //money += parseFloat(oneRedio[key].num) * parseFloat(oneRedio[key].Products_Price)
            }
            //console.log('money', money)
            for (var key in oneRedio) {
               secList[key] = oneRedio[key]
            }

            //console.log('oneRedio1===', oneRedio)            
            _this.setData({
               allRadio: 1,
               productRadio: Radio,
               isEditBtn: true,
               totalCounts: secList.length,
               
            })
         } else {
            for (var key in oneRedio) {
               Radio.push(0)
            }
            secList = []
            _this.setData({
               allRadio: 0,
               productRadio: Radio,
               totalCounts: 0,
               totalProducts: 0
            })
         }

      } else { //多选         
         var Radio = _this.data.productRadio         
        // console.log('oneRedio', oneRedio)
         var money = 0
         console.log('Radio', Radio)

         // if (_data.productRadio[index] == 0){
         for (var key in oneRedio) {
            if (index == key) {
               //console.log('_data.productRadio', _data.productRadio)
               Radio.splice(key, 1, Radio[key] ? 0 : 1)
               if (_data.productRadio[key] == 1) {
                  //console.log('secList', secList)
                  //console.log('oneRedio', oneRedio)
                  secList.push(oneRedio[key])                  
               } else {
                  console.log('secList', secList)
                  for(var i in secList){
                     if (secList[i].Cart_ID == oneRedio[key].Cart_ID)
                     secList.splice(i, 1)
                  }
               }
            } else {

            }
         }         
         console.log('secList', secList)
         _this.setData({
            productRadio: Radio,
            isEditBtn: true,
            totalCounts: secList.length,
            totalProducts: money
         })
         
         if (secList.length == oneRedio.length) {
            _this.setData({
               allRadio: 1,
            })
         } else {
            _this.setData({
               allRadio: 0,
            })
         }
      }
      for(var key in secList){
         money +=parseFloat(secList[key].num)*parseFloat(secList[key].Products_Price)
      }
      _this.setData({
         totalProducts: money
      })
     

   },
   // // 获取购物车数据
   getShopingCart() {
      var param = {
         User_ID: app.Data.userInfo.User_ID,
      }
      if(!co.empty(param.User_ID)){
         co.getMealApi('jyhs/Cartmg/getShopCart', param, function (res) {
            console.log('购物车', res.data)
            if (res.code == 29348) {
               _this.setData({
                  empty: false,
                  isShow: true,
                  info: res.msg,
                  order: [],
                  showOrder: []
               })
            } else if (res.code == 0) {
               var res1 = res.data.CartList
               var Radio = []
               for (var key in res1) {
                  Radio.push(0)
               }
               _this.setData({
                  empty: true,
                  isShow: false,
                  order: res.data.CartList,
                  showOrder: [],
                  productRadio: Radio,
                  userOrder: res.data
               })
            }
         })
      }else{
         co.toastAllText("请先登录哦~")
      }
   },
   onUnload: function() {
      secList = []
      // _this.setData({

      // })
   },
})